package com.leon.alishop.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.leon.alishop.dao.GoodsInfoDao;
import com.leon.alishop.dao.ProviderInfoDao;
import com.leon.alishop.spider.service.TaobaoGoodsService;
import com.leon.alishop.spider.spider.CrawlAlibabaSpider;
import com.leon.alishop.spider.spider.CrawlTaobaoSpider;
import com.leon.alishop.spider.worker.AlibabaShopGoodsStatusCrawlThread;
import com.leon.alishop.spider.worker.TaobaoShopCrawlThread;

@Controller
@RequestMapping(value = "/spider")
public class SpiderController extends BaseController{

	private static String SUCCESS_RESULT = "/spider/spider";
	
	@RequestMapping(value = "/info")
	public String exeInfo(HttpServletRequest req,HttpServletResponse res,Model model){
		model.addAttribute("status", TaobaoShopCrawlThread.isRun());
		return SUCCESS_RESULT;
	}
	
	@RequestMapping(value = "/start")
	public String exeStart(@RequestParam Integer goon,HttpServletRequest req,HttpServletResponse res,Model model){
		TaobaoShopCrawlThread job = new TaobaoShopCrawlThread();
		job.setCrawlTaobaoSpider(crawlTaobaoSpider);
		job.setTaobaoGoodsService(taobaoGoodsService);
		job.setProviderInfoDao(providerInfoDao);
		job.init(goon!=null && goon.intValue()==1);
		Thread t = new Thread(job);
		t.start();
		return "redirect:/spider/info.htm";
	}
	
	@RequestMapping(value = "/start-ali")
	public String exeStartAli(@RequestParam String from,HttpServletRequest req,HttpServletResponse res,Model model){
		AlibabaShopGoodsStatusCrawlThread job = new AlibabaShopGoodsStatusCrawlThread();
		job.setCrawlTaobaoSpider(crawlTaobaoSpider);
		job.setTaobaoGoodsService(taobaoGoodsService);
		job.setCrawlAlibabaSpider(crawlAlibabaSpider);
		job.setGoodsInfoDao(goodsInfoDao);
		job.setFromAlibabaWeb("web".equalsIgnoreCase(from));
		Thread t = new Thread(job);
		t.start();
//		t.run();
		return "redirect:/spider/info.htm";
	}
	
	@Autowired
	private CrawlTaobaoSpider crawlTaobaoSpider;
	@Autowired
	private CrawlAlibabaSpider crawlAlibabaSpider;
	@Autowired
	private GoodsInfoDao goodsInfoDao;
	@Autowired
	private TaobaoGoodsService taobaoGoodsService;
	@Autowired
	private ProviderInfoDao providerInfoDao;
	
}
